package com.eascs.web.o2o.web.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

import javax.inject.Inject;

import org.springframework.stereotype.Service;

import com.eascs.app.dao.PageResult;
import com.eascs.app.dao.SqlPageQuery;
import com.eascs.common.util.StringUtil;
import com.eascs.web.o2o.web.dao.BaseDao;
import com.eascs.web.o2o.web.entity.WfEmail;
import com.eascs.web.o2o.web.entity.WfMessage;
import com.eascs.web.o2o.web.form.MailForm;
import com.eascs.web.o2o.web.form.SmsForm;
import com.eascs.web.o2o.web.service.IMessageService;

@Service
public class MessageServiceImpl implements IMessageService{

	@Inject
	BaseDao baseDao;
	
	
	
	
	@Override
	public PageResult<Object[]> findMailBaseInfo(MailForm vo) {
		
		SqlPageQuery<Object[]> sqlPageQuery = new SqlPageQuery<>(Object[].class, vo.getPageSize(), vo.getShowPage());
		
		StringBuilder sql = new StringBuilder();
		sql.append("select t.email_id,t.system_code ,t.request_id,t.custom_type_code,"
				+ " case when t.mail_send_type='INSTANT' then '及时发送' when t.mail_send_type='SCHEDULE' then '定时发送'  else '常规发送' end ,"
				+ " t.title,t.create_time, case when t.status='0' then '未发送' else '已发送' end        from wf_email t  where 1=1");
		
		//请求系统编码
		if (StringUtil.isNotBlank(vo.getSystemCode())) {
			sql.append(" and t.system_code = ?");
			sqlPageQuery.addParam(vo.getSystemCode());
		}
		//发送类型
		if(StringUtil.isNotBlank(vo.getMailSendType())){
			sql.append(" AND t.mail_send_type = ?");
			sqlPageQuery.addParam(vo.getMailSendType());
		}
		//发送状态
		if(StringUtil.isNotBlank(vo.getStatus())){
			sql.append(" AND t.status = ?");
			sqlPageQuery.addParam(vo.getStatus());
		}
		//请求系统流水
		if(StringUtil.isNotBlank(vo.getRequestId())){
			sql.append(" AND t.request_id = ?");
			sqlPageQuery.addParam(vo.getRequestId());
		}
		//邮件类型编码
		if(StringUtil.isNotBlank(vo.getCustomTypeCode())){
			sql.append(" AND t.custom_type_code = ?");
			sqlPageQuery.addParam(vo.getCustomTypeCode());
		}
		
		if(StringUtil.isNotBlank(vo.getBeginTime())){
			sql.append(" AND t.create_time >= ?");
			SimpleDateFormat s=new SimpleDateFormat("yyyy-MM-dd");
			try {
				sqlPageQuery.addParam(s.parse(vo.getBeginTime()));
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		if(StringUtil.isNotBlank(vo.getEndTime())){
			sql.append(" AND t.create_time <= ?");
			SimpleDateFormat s=new SimpleDateFormat("yyyy-MM-dd");
			try {
				sqlPageQuery.addParam(s.parse(vo.getEndTime()));
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		//邮件标题
		if(StringUtil.isNotBlank(vo.getTitle())){
			sql.append(" AND t.title like  ? ");
			sqlPageQuery.addLikeParam(vo.getTitle());
		}
		sql.append(" order by t.create_time desc ");
  		sqlPageQuery.setQuerySql(sql.toString());
		PageResult<Object[]> customerBasepage = baseDao.findDataWithPageByWorkflow(sqlPageQuery);
		return customerBasepage;
	}
	
	
	
	@Override
	public List<Object[]> findEmailSystemType(){
	   return baseDao.findObjectsListByWorkflow("select DISTINCT system_code from wf_email where system_code is not null and   system_code<> ''  GROUP BY  system_code ");
	}
	
	
	
	@Override
	public WfEmail findDetailByEmailId(String emailId){
	   return baseDao.findEntityByWorkflow("select * from wf_email where email_id = ? ", WfEmail.class, emailId);
	}

	@Override
	public PageResult<Object[]> getSmsInfo(SmsForm smsForm) {
		SqlPageQuery<Object[]> sqlPageQuery = new SqlPageQuery<>(Object[].class, smsForm.getPageSize(), 
				smsForm.getIndex());
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT wf.request_id,wf.system_code,wf.biz_code,wf.send_type,wf.receive_telphone,wf.create_time,wf.STATUS FROM( SELECT w.content,w.request_id, w.system_code,w.biz_code, ( CASE WHEN w.send_type = 'INSTANT' THEN '即时发送' WHEN w.send_type = 'SCHEDULE' THEN '定时发送' WHEN w.send_type = 'GENERAL' THEN '常规发送' ELSE '测试发送' END ) AS 'send_type', w.receive_telphone, w.create_time, ( CASE WHEN w. STATUS = 1 THEN '已发送' ELSE '未发送' END ) AS STATUS FROM wf_message w ) wf WHERE 1 = 1");
		
		if(StringUtil.hasText(smsForm.getRequestId())){
			sql.append(" AND wf.request_id = ? ");
			sqlPageQuery.addParam(smsForm.getRequestId());
		}
		if(StringUtil.hasText(smsForm.getSystemCode())){
			sql.append(" AND wf.system_code = ? ");
			sqlPageQuery.addParam(smsForm.getSystemCode());
		}
		if(StringUtil.hasText(smsForm.getReceiveTelphone())){
			sql.append(" AND wf.receive_telphone = ? ");
			sqlPageQuery.addParam(smsForm.getReceiveTelphone());
		}
		if(!StringUtil.isEmpty(smsForm.getStatus())){
			sql.append(" AND wf.STATUS = ? ");
			sqlPageQuery.addParam(smsForm.getStatus());
		}
		if(!StringUtil.isEmpty(smsForm.getStartTime())){
			sql.append(" AND wf.create_time >= ? ");
			sqlPageQuery.addParam(smsForm.getStartTime());
		}
		if(!StringUtil.isEmpty(smsForm.getEndTime())){
			sql.append(" AND wf.create_time < date_add(?, INTERVAL 1 DAY) ");
			sqlPageQuery.addParam(smsForm.getEndTime());
		}
		if(StringUtil.hasText(smsForm.getContent())){
			sql.append(" AND wf.content LIKE ? ");
			sqlPageQuery.addLikeParam(smsForm.getContent());
		}
		sql.append(" ORDER BY wf.create_time DESC ");
		sqlPageQuery.setQuerySql(sql.toString());
		
		return baseDao.findDataWithPageByWorkflow(sqlPageQuery);
	}

	@Override
	public WfMessage findByrequestId(String requestId) {
		String sql = "SELECT * from wf_message  WHERE request_id = ?";
		return baseDao.findEntityByWorkflow(sql, WfMessage.class, requestId);
	}
	
	
	
}
